home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / Messages / Example1B.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  5KB  |  160 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM System                  Amiga C Club       */
  7. /* Chapter: Messages                    Tulevagen 22       */
  8. /* File:    Example1B.c                 181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program will create a message of type NrMessage.    */
  21. /* It will then try to find a message port called "NrPort". */
  22. /* If it finds that port it will send a message to it, and  */
  23. /* wait for the other task to reply.                        */
  24.  
  25.  
  26.  
  27. #include <exec/types.h>  /* STRPTR         */
  28. #include <exec/ports.h>  /* struct Message */
  29. #include <exec/memory.h> /* MEMF_PUBLIC    */
  30. #include <exec/nodes.h>  /* NT_MESSAGE     */
  31.  
  32.  
  33.  
  34. /* Declare a pointer to our message and reply port: */
  35. struct MsgPort *msgp, *replymsgp;
  36.  
  37. /* The message will be in this form: */
  38. struct NrMessage
  39. {
  40.   struct Message SystemMsg;
  41.   int number;
  42. };
  43.  
  44. /* Declare a pointer to the message: */
  45. struct NrMessage *nrmsg, *msg;
  46.  
  47.  
  48.  
  49. /* Declare the functions: */
  50. void clean_up();
  51. void main();
  52.  
  53.  
  54. void main()
  55. {
  56.   /* Allocate memory for the message: */
  57.   /* (Make it public and clear it.)   */
  58.   nrmsg = (struct NrMessage *)
  59.      AllocMem( sizeof( struct NrMessage ), MEMF_PUBLIC|MEMF_CLEAR );
  60.  
  61.   /* Check if we have allocated the memory successfully: */
  62.   if( !nrmsg )
  63.     clean_up( "Not enough memory for message!" );
  64.  
  65.  
  66.  
  67.   /* Try to find the message port "NrPort": */
  68.   msgp = (struct MsgPort *) FindPort( "NrPort" );
  69.  
  70.   /* Have we found the message port? */
  71.   if( !msgp )
  72.     clean_up( "Could not find the message port!" );
  73.  
  74.  
  75.  
  76.   /* Create a reply port: (When the other task replies */
  77.   /* we will receive a message at this port.)          */
  78.   replymsgp = (struct MsgPort *) CreatePort( "NrReplyPort", 0 );
  79.  
  80.   /* Have we received a reply port? */
  81.   if( !replymsgp )
  82.     clean_up( "Could not create the reply port!" );
  83.  
  84.  
  85.  
  86.   /* Initialize the message: */
  87.   /* Set type to NT_MESSAGE: */
  88.   nrmsg->SystemMsg.mn_Node.ln_Type = NT_MESSAGE;
  89.  
  90.   /* Give the message a pointer to our reply port: */
  91.   nrmsg->SystemMsg.mn_ReplyPort = replymsgp;
  92.  
  93.   /* Set the length of our message: */
  94.   nrmsg->SystemMsg.mn_Length = sizeof( struct NrMessage );
  95.  
  96.   /* Set the message itself: */
  97.   nrmsg->number = 12345;
  98.  
  99.  
  100.  
  101.   /* Inform the user: */
  102.   printf( "B: We have now successfully prepared everything!\n" );
  103.   printf( "B: Lets send the value: %d\n", nrmsg->number );
  104.  
  105.   /* Send the message: */  
  106.   PutMsg( msgp, nrmsg );
  107.  
  108.   /* After we have sent our message and until we */
  109.   /* receive a reply we are not allowed to read  */
  110.   /* or alter the message any more!              */
  111.   printf( "B: Message sent, lets wait...\n" );
  112.  
  113.  
  114.  
  115.   /* Wait at our reply port: */
  116.   WaitPort( replymsgp );
  117.  
  118.  
  119.  
  120.   /* Try to  receive as many messages as possible  */
  121.   /* from the reply port. (In this example we will */
  122.   /* only receive one reply, but you never know.)  */
  123.   while( msg = (struct NrMessage *) GetMsg( replymsgp ))
  124.   {
  125.     /* We may now examine the message: */
  126.     printf( "B: We have received a reply!\n" );
  127.     printf( "B: The value is now: %d\n", msg->number  );
  128.   }
  129.  
  130.  
  131.  
  132.   /* The End! */
  133.   clean_up( "The End!" );
  134. }
  135.  
  136.  
  137.  
  138. void clean_up( text )
  139. STRPTR text;
  140. {
  141.   /* If we have successfully allocated memory for a message */
  142.   /* deallocate it. (Note that we may not deallocate this   */
  143.   /* message if we have sent it to another task's message   */
  144.   /* port, and have not received any reply!)                */
  145.   if( nrmsg )
  146.     FreeMem( nrmsg, sizeof( *nrmsg ) );
  147.  
  148.   /* If we have we successfully created a reply */
  149.   /* port, close it:                            */
  150.   if( replymsgp )
  151.     DeletePort( replymsgp);
  152.  
  153.   /* Print any message: */
  154.   printf( "B: %s\n", text );
  155.  
  156.   /* Quit: */
  157.   exit( 0 );
  158. }
  159.  
  160.